package com.srwl.mytx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.bm.library.Info;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.BigPictureAdapterV2;
import com.srwl.mytx.domain.MyFile;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * recyclerView 展示大图，
 * 因为设置了缩放效果，recyclerView 复用的时候，会导致显示大小混乱，
 * 禁用 recyclerView的复用的话，翻页的时候，感觉加载不够及时，没有预加载一样，会出现黑边
 */
public class BigPicturePageViewV2 extends FrameLayout implements BigPictureAdapterV2.InteractionListener {
    @BindView(R.id.rv_big_picture)
    RecyclerView rv_bigPicture;
    @BindView(R.id.fl_bigPicture)
    View fl_bigPicture;
    @BindView(R.id.img_bg_black)
    ImageView iv_background;
    private BigPictureAdapterV2 bigPictureAdapter;
    private Context context;
    private AlphaAnimation in = new AlphaAnimation(0, 1);
    private AlphaAnimation out = new AlphaAnimation(1, 0);
    private GetPhotoViewInfoListener getPhotoViewInfoListener;
    private List<MyFile> myFileList = new ArrayList<>();
    private List<String> imagePathsCover = new ArrayList<>();
    private Info info;
    private boolean isShowAnimate;
    private int targetPosition;

    public BigPicturePageViewV2(@NonNull Context context) {
        super(context);
    }

    public BigPicturePageViewV2(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        inflate(context, R.layout.frame_layout_big_image_v1, this);
        ButterKnife.bind(this);
        //大图黑色背景显示隐藏 持续时间
        in.setDuration(400);
        in.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

                iv_background.setVisibility(View.VISIBLE);
                //bigPictureAdapter.showAnimateIn(info);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        out.setDuration(400);
        out.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                // bigPictureAdapter.showAnimateOut(info);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                iv_background.setVisibility(View.INVISIBLE);
                setVisibility(GONE);
                isShowAnimate = false;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        PagerSnapHelper snapHelper = new PagerSnapHelper() {

//            @Nullable
//            @Override
//            public View findSnapView(RecyclerView.LayoutManager layoutManager) {
//                Log.d(TAG, "findSnapView: 获取页面当前显示的页面");
//                snapView = super.findSnapView(layoutManager);
//                return snapView;
//            }

            @Override
            public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
                int targetSnapPosition = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
                if (targetSnapPosition >= 0 && targetSnapPosition <= myFileList.size() - 1) {
                    targetPosition = targetSnapPosition;
                }
                return targetSnapPosition;
            }
        };
        snapHelper.attachToRecyclerView(rv_bigPicture);
        //rv_bigPicture.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false));
        rv_bigPicture.setLayoutManager(new LinearLayoutManager(context));

    }

    public void init(List<MyFile> myFileList, GetPhotoViewInfoListener getPhotoViewInfoListener) {
        this.myFileList = myFileList;
        // imagePathsCover.addAll(myFileList);
        this.getPhotoViewInfoListener = getPhotoViewInfoListener;
        bigPictureAdapter = new BigPictureAdapterV2(context, myFileList, this);
        rv_bigPicture.setAdapter(bigPictureAdapter);
    }


    public void showBigPictures(int currentItemPosition, Info info) {
        this.info = info;
        setVisibility(VISIBLE);
        bigPictureAdapter.setInfo(info);
        // imagePathsCover.addAll(myFileList);
        bigPictureAdapter.notifyDataSetChanged();
        rv_bigPicture.scrollToPosition(currentItemPosition);
        iv_background.startAnimation(in);

    }


    public void hideBigPictures(Info info) {
        isShowAnimate = true;
        this.info = info;
        iv_background.startAnimation(out);
        // setVisibility(View.GONE);

    }

    public void refresh() {
        bigPictureAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int position) {
        Info info = getPhotoViewInfoListener.onGetPhotoViewInfo(position);
        hideBigPictures(info);

    }

    @Override
    public void onItemRemove(int position) {

    }

    @Override
    public Info onGetImageRectInfo(int position) {
        Info info = getPhotoViewInfoListener.onGetPhotoViewInfo(position);
        hideBigPictures(info);
        return info;
    }


    public boolean isShowAnimate() {
        return isShowAnimate;
    }

    public int getCurrentItem() {
        return targetPosition;
    }


    public interface GetPhotoViewInfoListener {
        Info onGetPhotoViewInfo(int position);
    }
}
